package com.heima.controller;

import com.heima.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    //简单类型接收url参数 方法一
    @RequestMapping("/text01")
    public ModelAndView text01(ModelAndView modelAndView,Integer id,String username){
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg","user_id:"+id+",username:"+username);
        modelAndView.setViewName("message");
        return modelAndView;
    }

    //简单类型接收url参数 方法二  如果参数名不同需要用@RequestParam注解指定参数名
    @RequestMapping("/text02")
    public ModelAndView text02(ModelAndView modelAndView, @RequestParam("user_id") Integer id, String username){
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg",String.format("id:%d,username:%s",id,username));
        modelAndView.setViewName("message");
        return modelAndView;
    }

    //POJO类接收请求参数
    @RequestMapping("/text03")
    public ModelAndView text03(ModelAndView modelAndView,User user){
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg",String.format("id:%d,username:%s",user.getId(),user.getUsername()));
        modelAndView.setViewName("message");
        return modelAndView;
    }

    //spring会将请求参数自动放到复杂类型中
    @RequestMapping("/text04")
    public ModelAndView text04(ModelAndView modelAndView,User user){
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg",user.getAccount());
        modelAndView.setViewName("message");
        return modelAndView;
    }

    //数组类型接收url参数
    @RequestMapping("/text05")
    public ModelAndView text05(ModelAndView modelAndView,String[] args){
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg", Arrays.toString(args));
        modelAndView.setViewName("message");
        return modelAndView;
    }

    //list集合类型接收url参数 必须使用@RequestParam注解绑定参数
    @RequestMapping("/text06")
    public ModelAndView text06(ModelAndView modelAndView,@RequestParam("list") List<String> list){
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg",list);
        modelAndView.setViewName("message");
        return modelAndView;
    }

    //类型转换  默认只能传2000/12/01 中间用/隔开的
    @RequestMapping("/getDate")
    public ModelAndView getDate(ModelAndView modelAndView,Date date){
        modelAndView.setViewName("message");
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg",date);
        return modelAndView;
    }

    //类型转换 注解解决不能用-的问题
    @RequestMapping("/getDate1")
    public ModelAndView getDate1(ModelAndView modelAndView,@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        modelAndView.setViewName("message");
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg",date);
        return modelAndView;
    }
}
